﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using FirebirdSql.Data.FirebirdClient;
using System.Configuration;
namespace University
{
    public partial class Main : Form
    {
      
           static public FbConnection con = new FbConnection("User = SYSDBA;" + "Password = masterkey;" +
                                            "Database =D:\\Навчання\\БД\\University_!\\University - копия\\Backup\\University\\UNIVERSITY.fdb;" +
                                             "Post = 3050;" + "Dialect=3;"+
                                             "Charset = NONE");
            public string  str = "User = SYSDBA;" + "Password = masterkey;" +
                                            "Database =D:\\Навчання\\БД\\University_!\\University - копия\\Backup\\University\\UNIVERSITY.fdb;" +
                                             "Post = 3050;" + "Dialect=3;" +
                                             "Charset = NONE";
            static string sqlSelect = "SELECT * FROM OBJECT  order by UNIQUE_NO";
       
           public  FbDataAdapter da = new FbDataAdapter(sqlSelect, con);
        
           public  DataTable dt = new DataTable();

           public Point p;
        public int class_id;

            private TreeNode nod = new TreeNode();
        List<int> list = new List<int>();
        public Main()
        {
           
            da.Fill(dt);
            
            InitializeComponent();

           
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            if (con.State != ConnectionState.Open)
            {
                con.Open();
            }
       
            foreach (DataRow row in dt.Rows)
            {
                if (Convert.ToInt32(row["MAJOUR"]) == 0 )

                {

                    TreeNode rootNode = new TreeNode(row["NAME"].ToString());
                    rootNode.Nodes.Add("");
                    rootNode.ForeColor = Color.Orange;
                    rootNode.Tag = row["UNIQUE_NO"].ToString();
                    treeView1.Nodes.Add(rootNode);
                    rootNode.ContextMenuStrip = contextMenuStrip1;
                   
                }
            }
            con.Close();
           // Forma();

        }
   

      protected void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
          //  treeView1.SelectedNode = e.Node;
        //    Forma(e.Node);
         
        }
         private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)
         {
             treeView1.SelectedNode = e.Node;
             e.Node.Nodes.Clear();
             InsertChild(e.Node);
         }

         
        private void treeView1_AfterExpand(object sender, TreeViewEventArgs e)
        {
           /* treeView1.SelectedNode = e.Node;
            e.Node.Nodes.Clear();
            InsertChild(e.Node);*/
        }

        private void treeView1_AfterCollapse(object sender, TreeViewEventArgs e)
        {
           
            treeView1.SelectedNode = e.Node;
            e.Node.Nodes.Clear();
            e.Node.Nodes.Add("");
        }
          
     public void InsertChild(TreeNode n)
        {
           
            dt.Clear();
            da.Fill(dt);
 
            if (con.State != ConnectionState.Open)
            {
                con.Open();
            }
            int key;
            key=Convert.ToInt32(n.Tag.ToString());
            foreach (DataRow row in dt.Rows)
            {
                if (Convert.ToInt32(row["MAJOUR"]) == key)
                {
                  TreeNode NewNode = new TreeNode(row["NAME"].ToString());
                    NewNode.Tag = row["UNIQUE_NO"].ToString();
                    if (check(NewNode) == true)
                   
                    {
                        NewNode.Nodes.Add("");
                    }
                   n.Nodes.Add(NewNode);
                 NewNode.ContextMenuStrip = contextMenuStrip1;
               
                  
                }
                
            }
            con.Close();
        }

        public bool check(TreeNode n)
        {
            if (con.State != ConnectionState.Open)
            {
                con.Open();
            }
            
             int c=0;
             int key;
            key=Convert.ToInt32(n.Tag.ToString());
            foreach (DataRow row in dt.Rows)
            {
              
                if (Convert.ToInt32(row["MAJOUR"]) == key)
                {
                    c++;
                }
            }
            if (c > 0) { return true; }
            else { return false; }
            //con.Close();
          
        }
        private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
           /*// treeView1.SelectedNode = e.Node;
           treeView1.SelectedNode.Nodes.Clear();

            InsertChild(treeView1.SelectedNode);
            if (e.Button == MouseButtons.Right)
            {

               
                Forma(e.Node);
                

            }*/
        }
        private void treeView1_MouseDown(object sender, MouseEventArgs e)
        {
            
            if (e.Button == MouseButtons.Right)
            {
                
                p = new Point(e.X, e.Y);
                treeView1.SelectedNode = treeView1.GetNodeAt(p);
               Forma(treeView1.SelectedNode);
               int k=0;
               string select = "SELECT CLASS FROM OBJECT where UNIQUE_NO=" + "'" + Convert.ToInt32(treeView1.SelectedNode.Tag.ToString()) + "'";
               FbDataAdapter adapt = new FbDataAdapter(select, con);
               DataTable table = new DataTable();
               adapt.Fill(table);
               for (int i = 36; i <= 53; i++)
               {
                   if (i == Convert.ToInt32(table.Rows[0][0].ToString())) { k = 1; }
               }
               if (Convert.ToInt32(table.Rows[0][0].ToString()) == 19) { k = 1; }
               if (k == 1) { showToolStripMenuItem1.Enabled = true; futureToolStripMenuItem.Enabled = true; }
               if (k == 0) { showToolStripMenuItem1.Enabled = false; futureToolStripMenuItem.Enabled = false; }
            }
        }

        private void Forma(TreeNode node)
        {
           
            if (con.State != ConnectionState.Open)
            {
                con.Open();
            }
          
            addToolStripMenuItem.DropDownItems.Clear();
           
   


            int i = 0;
         
            string select = "SELECT TREFERENCES.FORM FROM RULE_INS,TREFERENCES where TREFERENCES.CLASS_NO=RULE_INS.CLASS_INSERT and RULE_INS.CLASS_ID=" + "'" + Convert.ToInt32(treeView1.SelectedNode.Tag.ToString()) + "'";

              FbDataAdapter adapt = new FbDataAdapter(select,con);
        
              DataTable table = new DataTable();
              adapt.Fill(table);

              foreach (DataRow rw in table.Rows)
                {

                   
                    addToolStripMenuItem.DropDownItems.Add(table.Rows[i][0].ToString(), null, ToolStripMenuItemClick);

                  i++;
                }
            
        }
        public void ToolStripMenuItemClick(object sender, EventArgs e)
        {
            if (con.State != ConnectionState.Open)
            {
                con.Open();
            }
            ToolStripMenuItem menu = (ToolStripMenuItem)sender;
            string name = menu.Text;
            name = name.Remove(name.IndexOf(" "));
            FbCommand cmd = new FbCommand(@"SELECT * FROM TREFERENCES where FORM='" + name + "'", con);
            FbDataReader rdr = cmd.ExecuteReader();
            rdr.Read();
            class_id = Convert.ToInt32(rdr["CLASS_NO"].ToString());
            Control myControl = new Control();
            Type typeofControl = Type.GetType("University." + name, true);
            myControl = (Control)(Activator.CreateInstance(typeofControl, treeView1.SelectedNode, contextMenuStrip1, "Add", Convert.ToInt32(rdr["CLASS_NO"].ToString())));
            myControl.Show();
            
            con.Close();

        }
        public int ClassReturn(string FormName)
        {
            if (con.State != ConnectionState.Open)
            {
                con.Open();
            }
            FbCommand cmd = new FbCommand(@"SELECT * FROM TREFERENCES where FORM=" + FormName, con);
            FbDataReader rdr = cmd.ExecuteReader();
            rdr.Read();
            return Convert.ToInt32(rdr["CLASS_NO"].ToString());
        }
      
      
        private void addToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
             define_form("Add");
        }

        public void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (check(treeView1.SelectedNode) == true)
            {
                MessageBox.Show("Видаліть спочатку дочірні підвузли!!!");
            }
            else
            {
                define_form("Remove");
            }
          
        }

        private void editToolStripMenuItem_Click(object sender, EventArgs e)
        {
            define_form("Edit");
        }
    
        private void define_form(string command)
        {
            if (con.State != ConnectionState.Open)
            {
                con.Open();
            }
         
            TreeNode s = treeView1.SelectedNode;
            FbCommand cmd = new FbCommand(@"SELECT * FROM OBJECT  where UNIQUE_NO=" + Convert.ToInt32(treeView1.SelectedNode.Tag.ToString()), con);
            FbDataReader rdr = cmd.ExecuteReader();
            rdr.Read();

            FbCommand cmd1 = new FbCommand(@"SELECT * FROM TREFERENCES where CLASS_NO=" + rdr["CLASS"].ToString(), con);
            FbDataReader rdr1 = cmd1.ExecuteReader();
            rdr1.Read();
            string name = rdr1["FORM"].ToString();
            name = name.Remove(name.IndexOf(" "));
            Control myControl = new Control();
            Type typeofControl = Type.GetType("University." + name, true);
            myControl = (Control)(Activator.CreateInstance(typeofControl, treeView1.SelectedNode, contextMenuStrip1, command, class_id));
            if (command != "Remove") { myControl.Show(); }
            con.Close();
            rdr1.Close();
            rdr.Close();

        }

        private void showToolStripMenuItem_Click(object sender, EventArgs e)
        {
            define_form("Show");
        }

        private void showToolStripMenuItem1_Click(object sender, EventArgs e)
        {
          
             if (con.State != ConnectionState.Open)
            {
                con.Open();
            }
            TreeNode s = treeView1.SelectedNode;
            FbCommand cmd = new FbCommand(@"SELECT * FROM OBJECT  where UNIQUE_NO=" + Convert.ToInt32(treeView1.SelectedNode.Tag.ToString()), con);
            FbDataReader rdr = cmd.ExecuteReader();
            rdr.Read();
            int rdr_class=Convert.ToInt32(rdr["CLASS"].ToString());
            rdr.Close();

          
            

            FbCommand cmd1 = new FbCommand(@"SELECT * FROM VUDATKY  where UNIQUE_NO=" + Convert.ToInt32(treeView1.SelectedNode.Tag.ToString()), con);
            FbDataReader rdr1 = cmd1.ExecuteReader();
            rdr1.Read();
            string adm=rdr1["ADMINISTRATUVNI"].ToString();
            string pod=rdr1["PODATKU"].ToString();
            string soc=rdr1["SOCZAHUST"].ToString();
            rdr1.Close();

            for (int i = 36; i <= 41; i++)
            {

                if (rdr_class == i)
                {
                    VNAYKOVI vn = new VNAYKOVI(adm, pod, soc, Convert.ToInt32(treeView1.SelectedNode.Tag.ToString()));
                    vn.Show();
                }

            }
            for (int i = 48; i <= 53; i++)
            {

                if (rdr_class == i)
                {
                    VGOSP vg = new VGOSP(adm, pod, soc, Convert.ToInt32(treeView1.SelectedNode.Tag.ToString()));
                    vg.Show();
                }

            }
            if (rdr_class == 19)
            {   
                VUDATKY v = new VUDATKY(adm, pod, soc, Convert.ToInt32(treeView1.SelectedNode.Tag.ToString()));
                v.Show(); 
            }
            if ((rdr_class == 42) || (rdr_class == 43) || (rdr_class == 44) || (rdr_class == 45) || (rdr_class == 46))
            {
                FbCommand cmd2 = new FbCommand(@"SELECT * FROM VNADZVUCHAJNI  where UNIQUE_NO=" + Convert.ToInt32(treeView1.SelectedNode.Tag.ToString()), con);
                FbDataReader rdr2 = cmd2.ExecuteReader();
                rdr2.Read();
                string zap = rdr2["ZAPLANOVANI"].ToString();
                string rel = rdr2["REALNI"].ToString();
                rdr2.Close();

                if (rdr_class == 42)
                {

                    VNADZVUCHAJNI vn = new VNADZVUCHAJNI(adm, pod, soc, Convert.ToInt32(treeView1.SelectedNode.Tag.ToString()), zap, rel);
                    vn.Show();
                }
                if ((rdr_class == 43) || (rdr_class == 45))
                {
                    VORG vo = new VORG(adm, pod, soc, Convert.ToInt32(treeView1.SelectedNode.Tag.ToString()), zap, rel);
                    vo.Show();
                }

                if ((rdr_class == 44)||(rdr_class == 46))
                {
                    FbCommand cmd3 = new FbCommand(@"SELECT * FROM VPROFKOM  where UNIQUE_NO=" + Convert.ToInt32(treeView1.SelectedNode.Tag.ToString()), con);
                    FbDataReader rdr3 = cmd3.ExecuteReader();
                    rdr3.Read();
                    string zah = rdr3["ZAHODY"].ToString();
                    rdr3.Close();
                    if (rdr_class == 44)
                    {
                        VSUM vs = new VSUM(adm, pod, soc, Convert.ToInt32(treeView1.SelectedNode.Tag.ToString()), zap, rel, zah);
                        vs.Show();
                    }
                    if (rdr_class == 46)
                    {
                        FbCommand cmd4 = new FbCommand(@"SELECT * FROM VPROFCOMSTUD  where UNIQUE_NO=" + Convert.ToInt32(treeView1.SelectedNode.Tag.ToString()), con);
                        FbDataReader rdr4 = cmd4.ExecuteReader();
                        rdr4.Read();
                        string dobr = rdr4["DOBROCHUNNIST"].ToString();
                        rdr4.Close();

                        VPROFCOMSTUD vps = new VPROFCOMSTUD(adm, pod, soc, Convert.ToInt32(treeView1.SelectedNode.Tag.ToString()), zap, rel, zah, dobr);
                        vps.Show();
                    }
                }
            }
            con.Close();
        }

        private void futureToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Future f = new Future();
            f.Show();
        }



    }
}